package com.fosss.band.controller;

import com.fosss.band.entity.vo.LoginUserVo;
import com.fosss.band.entity.vo.RegisterUserVo;
import com.fosss.band.entity.vo.UserInfoVo;
import com.fosss.band.service.UserService;
import com.fosss.band.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author: fosss
 * Date: 2023/12/27
 * Time: 21:36
 * Description:
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
@Slf4j
//@CrossOrigin//跨域
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private OssUtil ossUtil;


    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginUserVo loginUser) {
        log.info("执行登录操作");
        return userService.login(loginUser);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public Result info(@RequestParam("token") String token) {
        log.info("获取用户信息:" + token);
        return userService.info(token);
    }

    @ApiOperation("用户登出")
    @PostMapping("/logout")
    public Result logout(@RequestParam("token") String token) {
        return userService.logout(token);
    }

    @ApiOperation("发送验证码")
    @GetMapping("/register/sendCode/{email}")
    public Result sendCode(@PathVariable("email") String email) {
        log.info("发送验证码");
        return userService.sendCode(email);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterUserVo registerUser) {
        log.info("注册用户：{}", registerUser);
        return userService.register(registerUser);
    }

    @ApiOperation("找回密码")
    @PostMapping("/forgetPassword")
    public Result forgetPassword(@RequestBody RegisterUserVo registerUser) {
        log.info("找回密码：{}", registerUser);
        return userService.forgetPassword(registerUser);
    }

    @ApiOperation("上传头像")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        String url = ossUtil.uploadFile(file);
        return Result.ok().data("url", url);
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/saveInfo")
    public Result saveInfo(@RequestBody UserInfoVo userInfoVo) {
        log.info("修改个人信息：{}", userInfoVo);
        return userService.saveInfo(userInfoVo);
    }

    @ApiOperation("验证验证码是否正确，直接把用户信息改掉")
    @GetMapping("/emailCodeRight/{email}/{code}")
    public Result emailCodeRight(@PathVariable("email") String email, @PathVariable("code") String code) {
        log.info("验证验证码是否正确");
        return userService.emailCodeRight(email, code);
    }

    @ApiOperation("根据用户名获取用户信息")
    @GetMapping("/getByUsername/{username}")
    public Result getByUsername(@PathVariable("username") String username) {
        log.info("根据用户名获取用户信息");
        return userService.getByUsername(username);
    }
}


















